package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;//套餐菜品表
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    CategoryService categoryService;

    /*
     *   新增套餐
     * */
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.addWithDish(setmealDto);
        return R.success("保存套餐成功！");
    }

    /*
     *   套餐分页查询
     * */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        setmealService.page(setmealPage, queryWrapper);
        //创建分页构造器
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        //分页页面基本数据拷贝
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        //获取分页数据中records
        List<Setmeal> records = setmealPage.getRecords();
        for (Setmeal record : records) {
            //遍历获取得到套餐分类id
            Long categoryId = record.getCategoryId();
            //调用套餐分类业务通过id获取具体套餐对象
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            if (categoryName != null) {
                //创建SetmealDto对象
                SetmealDto setmealDto = new SetmealDto();
                //把套餐分类名称放入
                setmealDto.setCategoryName(categoryName);
                //把record拷贝SetmealDto对象
                BeanUtils.copyProperties(record, setmealDto);
                setmealDtos.add(setmealDto);
            }
        }
        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    /*
     *   删除套餐
     * */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }


    /*
     *   起售/停售套餐
     * */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.set(Setmeal::getStatus, status);
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.update(lqw);
        }
        return R.success("操作成功");
    }

    /*
     *   根据套餐id查询套餐数据
     * */
    @GetMapping("/{id}")
    public R<SetmealDto> select(@PathVariable Long id) {
        //通过套餐id查询具体套餐
        Setmeal setmeal = setmealService.getById(id);
        //根据套餐菜品关系表 查询对应套餐的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        //查询获取套餐内菜品集合
        List<SetmealDish> list = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        //拷贝到SetmealDto
        return R.success(setmealDto);
    }

    /*
     *   修改套餐
     * */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /*
     *   根据套餐分类id查询该套餐分类下的套餐
     * */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,1);
        List<Setmeal> setmealList = setmealService.list(qw);
        return R.success(setmealList);
    }
}
